package com.example.appguangbo2;

import androidx.appcompat.app.AppCompatActivity;
import androidx.localbroadcastmanager.content.LocalBroadcastManager;

import android.content.BroadcastReceiver;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
    public static final String BROADCAST_ACTION="BROADCAST_ACTION";


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        init();

    }
    private void init(){
        //注册应用内广播接收器
//步骡1：实例化BroadcastReceiver子类&IntentFilter mBroadcastReceiver
        BroadcastReceiver mBroadcastReceiver= new mBroadcastReceiver();
        IntentFilter intentFilter =new IntentFilter();
//步骤2：实例化LocalBroadcastManager的实例
//步骤3：设置接收广播的类型
        intentFilter.addAction(BROADCAST_ACTION);
//步骤4：调用LocalBroadcastManager.单一实例的registerReceiver()方法进行动态注册
        LocalBroadcastManager.getInstance(this).registerReceiver(mBroadcastReceiver,intentFilter);
//取消注册应用内广播接收器
        LocalBroadcastManager.getInstance(this).unregisterReceiver(mBroadcastReceiver);
//发送应用内广播
        Intent intent= new Intent();
        intent.setAction(BROADCAST_ACTION);
        LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
    }
}